﻿using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;

namespace QRCodeNet
{
	public partial class SettingDlg : Form
	{
		public SettingDlg()
		{
			InitializeComponent();
			tbEachRow.DataBindings.Add("Text", Config.config, "EachRow", true);
			tbEachCol.DataBindings.Add("Text", Config.config, "EachCol", true);
			tbQRWid.DataBindings.Add("Text", Config.config, "QRWid", true);
			tbQRHei.DataBindings.Add("Text", Config.config, "QRHei", true);
			tbXOffset.DataBindings.Add("Text", Config.config, "XOffset", true);
			tbYOffset.DataBindings.Add("Text", Config.config, "YOffset", true);
			tbHistoryNum.DataBindings.Add("Text", Config.config, "HistoryNum", true);

			tbBrdWid.DataBindings.Add("Text", Config.config, "BrdWid", true);
			tbBrdHei.DataBindings.Add("Text", Config.config, "BrdHei", true);

			tbNo.DataBindings.Add("Text", Config.config, "No", true);
			tbColor.DataBindings.Add("Text", Config.config, "Color", true);
			tbStd.DataBindings.Add("Text", Config.config, "Std", true);
			tbFootSize.DataBindings.Add("Text", Config.config, "FootSize", true);
			tbProName.DataBindings.Add("Text", Config.config, "ProName", true);
			tbMaterial.DataBindings.Add("Text", Config.config, "Material", true);
			tbLevel.DataBindings.Add("Text", Config.config, "Level", true);

			tbEAN13.DataBindings.Add("Text", Config.config, "EAN13", true);
			tbBarInit.DataBindings.Add("Text", Config.config, "BarInit", true);
			tbNoInit.DataBindings.Add("Text", Config.config, "NoInit", true);

			tbCount.DataBindings.Add("Text", Config.config, "Count", true);
			tbEachBox.DataBindings.Add("Text", Config.config, "EachBox", true);
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (Config.config.Save())
			{
				DialogResult = DialogResult.OK;
				Close();
			}
			else
			{
				MessageBox.Show("保存文件失败");
			}
		}

		private void btnPreview_Click(object sender, EventArgs e)
		{
			Mat m = new Mat(230, 500, DepthType.Cv8U, 3);
			m.SetTo(new MCvScalar(255, 255, 255));
			Color color = Color.Black;
			int margin = 20;
			int FromLeft = 250;
			int RowMargin = 35;
			CvInvoke.Line(m, new Point(margin, margin + (int)(RowMargin * 1.7)), new Point(500 - margin, margin + (int)(RowMargin * 1.7)), new MCvScalar(0, 0, 0), 2);
			Emgu.CV.Extension.CvInvoke.PutText(m, "产品编号：", new Point(margin, margin), 1.2, color);
			Emgu.CV.Extension.CvInvoke.PutText(m, Config.config.No, new Point(margin + 35, margin + RowMargin), 1.2, color);
			Emgu.CV.Extension.CvInvoke.PutText(m, $"颜色：{Config.config.Color}", new Point(margin, margin + RowMargin * 2), 1.2, color);
			Emgu.CV.Extension.CvInvoke.PutText(m, $"品名：{Config.config.ProName}", new Point(margin, margin + RowMargin * 3), 1.2, color);
			Emgu.CV.Extension.CvInvoke.PutText(m, "执行标准：", new Point(margin, margin + RowMargin * 4), 1.2, color);
			Emgu.CV.Extension.CvInvoke.PutText(m, Config.config.Std, new Point(margin + 35, margin + RowMargin * 5), 1, color);

			Emgu.CV.Extension.CvInvoke.PutText(m, $"EUR：{Config.config.FootSize}", new Point(FromLeft, margin), 1.2, color);
			Emgu.CV.Extension.CvInvoke.PutText(m, $"材料：{Config.config.Material}", new Point(FromLeft, margin + RowMargin * 2), 1.2, color);
			Emgu.CV.Extension.CvInvoke.PutText(m, $"等级：{Config.config.Level}", new Point(FromLeft, margin + RowMargin * 3), 1.2, color);
			BarcodeWriter bw = new BarcodeWriter()
			{
				Format = BarcodeFormat.EAN_13,
				Options = new ZXing.Common.EncodingOptions()
				{
					Height = 50,
					Width = 500 - FromLeft,
					PureBarcode = false
				}
			};
			Bitmap b = null;
			try
			{
				b = bw.Write(Config.config.EAN13);
			}
			catch (System.ArgumentException)
			{
				MessageBox.Show("无效的条形码");
				return;
			}
			Image<Bgr, byte> ean13 = new Image<Bgr, byte>(b);
			ean13.Mat.CopyTo(new Mat(m, new Rectangle(new Point(FromLeft, margin + RowMargin * 4), ean13.Size)));
			CvInvoke.NamedWindow("Preview");
			CvInvoke.Imshow("Preview", m);
			CvInvoke.WaitKey(0);
		}

	}
}
